package com.gradecak.alfresco.mvc.servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.Principal;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletInputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.extensions.webscripts.servlet.WebScriptServletRequest;

public class FrameworkServletRequestProxy implements HttpServletRequest {

	private static final Log logger = LogFactory.getLog(FrameworkServletRequestProxy.class);

	private HttpServletRequest httpServletRequest;
	private WebScriptServletRequest webScriptServletRequest;

	public FrameworkServletRequestProxy(WebScriptServletRequest webScriptServletRequest) {
		this.webScriptServletRequest = webScriptServletRequest;
		this.httpServletRequest = webScriptServletRequest.getHttpServletRequest();
	}

	public WebScriptServletRequest getWebScriptServletRequest() {
		return webScriptServletRequest;
	}

	public void setCharacterEncoding(String env) throws UnsupportedEncodingException {
		httpServletRequest.setCharacterEncoding(env);
	}

	public void setAttribute(String name, Object o) {
		httpServletRequest.setAttribute(name, o);
	}

	public void removeAttribute(String name) {
		httpServletRequest.removeAttribute(name);
	}

	public boolean isSecure() {
		return httpServletRequest.isSecure();
	}

	public int getServerPort() {
		return httpServletRequest.getServerPort();
	}

	public String getServerName() {
		return httpServletRequest.getServerName();
	}

	public String getScheme() {
		return httpServletRequest.getScheme();
	}

	public RequestDispatcher getRequestDispatcher(String path) {
		return httpServletRequest.getRequestDispatcher(path);
	}

	public int getRemotePort() {
		return httpServletRequest.getRemotePort();
	}

	public String getRemoteHost() {
		return httpServletRequest.getRemoteHost();
	}

	public String getRemoteAddr() {

		return httpServletRequest.getRemoteAddr();
	}

	public String getRealPath(String path) {

		return httpServletRequest.getRealPath(path);
	}

	public BufferedReader getReader() throws IOException {

		return httpServletRequest.getReader();
	}

	public String getProtocol() {

		return httpServletRequest.getProtocol();
	}

	public String[] getParameterValues(String name) {

		return httpServletRequest.getParameterValues(name);
	}

	public Enumeration getParameterNames() {

		return httpServletRequest.getParameterNames();
	}

	public Map getParameterMap() {

		return httpServletRequest.getParameterMap();
	}

	public String getParameter(String name) {

		return httpServletRequest.getParameter(name);
	}

	public Enumeration getLocales() {

		return httpServletRequest.getLocales();
	}

	public Locale getLocale() {

		return httpServletRequest.getLocale();
	}

	public int getLocalPort() {

		return httpServletRequest.getLocalPort();
	}

	public String getLocalName() {

		return httpServletRequest.getLocalName();
	}

	public String getLocalAddr() {

		return httpServletRequest.getLocalAddr();
	}

	public ServletInputStream getInputStream() throws IOException {

		return httpServletRequest.getInputStream();
	}

	public String getContentType() {

		return httpServletRequest.getContentType();
	}

	public int getContentLength() {

		return httpServletRequest.getContentLength();
	}

	public String getCharacterEncoding() {

		return httpServletRequest.getCharacterEncoding();
	}

	public Enumeration getAttributeNames() {

		return httpServletRequest.getAttributeNames();
	}

	public Object getAttribute(String name) {

		return httpServletRequest.getAttribute(name);
	}

	public boolean isUserInRole(String role) {

		return httpServletRequest.isUserInRole(role);
	}

	public boolean isRequestedSessionIdValid() {

		return httpServletRequest.isRequestedSessionIdValid();
	}

	public boolean isRequestedSessionIdFromUrl() {

		return httpServletRequest.isRequestedSessionIdFromUrl();
	}

	public boolean isRequestedSessionIdFromURL() {

		return httpServletRequest.isRequestedSessionIdFromURL();
	}

	public boolean isRequestedSessionIdFromCookie() {

		return httpServletRequest.isRequestedSessionIdFromCookie();
	}

	public Principal getUserPrincipal() {

		return httpServletRequest.getUserPrincipal();
	}

	public HttpSession getSession(boolean create) {

		return httpServletRequest.getSession(create);
	}

	public HttpSession getSession() {

		return httpServletRequest.getSession();
	}

	public String getServletPath() {
		return "";
	}

	public String getRequestedSessionId() {

		return httpServletRequest.getRequestedSessionId();
	}

	public StringBuffer getRequestURL() {

		return httpServletRequest.getRequestURL();
	}

	public String getRequestURI() {
		String uri = httpServletRequest.getRequestURI();
		Pattern p = Pattern.compile("(^" + webScriptServletRequest.getServiceContextPath() + "/)(.*)(/"
				+ webScriptServletRequest.getExtensionPath() + ")");
		Matcher m = p.matcher(uri);

		if (m.find()) {
			try {
				return m.group(3);
			} catch (Exception e) {
				// let an empty string be returned
				logger.warn("no such group (3) in regexp while URI evaluation", e);
			}
		}

		return "";
	}

	public String getRemoteUser() {

		return httpServletRequest.getRemoteUser();
	}

	public String getQueryString() {

		return httpServletRequest.getQueryString();
	}

	public String getPathTranslated() {

		return httpServletRequest.getPathTranslated();
	}

	public String getPathInfo() {

		return httpServletRequest.getPathInfo();
	}

	public String getMethod() {

		return httpServletRequest.getMethod();
	}

	public int getIntHeader(String name) {

		return httpServletRequest.getIntHeader(name);
	}

	public Enumeration getHeaders(String name) {

		return httpServletRequest.getHeaders(name);
	}

	public Enumeration getHeaderNames() {

		return httpServletRequest.getHeaderNames();
	}

	public String getHeader(String name) {

		return httpServletRequest.getHeader(name);
	}

	public long getDateHeader(String name) {

		return httpServletRequest.getDateHeader(name);
	}

	public Cookie[] getCookies() {

		return httpServletRequest.getCookies();
	}

	public String getContextPath() {
		return "";
	}

	public String getAuthType() {

		return httpServletRequest.getAuthType();
	}

	@Override
	public String toString() {
		return getRequestURI();
	}
	
	public HttpServletRequest getHttpServletRequest() {
		return httpServletRequest;
	}
}
